package com.yicoollxing.service.impl;

import java.net.ResponseCache;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.yicoollxing.common.ResponseCode;
import com.yicoollxing.common.ServerResponse;
import com.yicoollxing.dao.mapper.CategoryMapper;
import com.yicoollxing.pojo.Category;
import com.yicoollxing.service.ICategoryService;

@Service("iCategoryService")
public class CategoryServiceImpl implements ICategoryService {

	@Autowired
	private CategoryMapper categoryMapper;

	public ServerResponse addCategory(
			String categoryName,
			@RequestParam(value = "parentId", defaultValue = "0") Integer parentId,
			Integer mid) {

		if (mid == null) {
			return ServerResponse.createByErrorCodeMessage(
					ResponseCode.ILLEGAL_ARGUMENT.getCode(),
					ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		Category category = new Category();
		category.setMid(mid);
		category.setName(categoryName);
		category.setParentId(parentId);
		category.setStatus(1);
		int resultraw = categoryMapper.insert(category);
		if (resultraw == 0) {
			return ServerResponse.createByErrorMessage("添加分类失败");
		}
		return ServerResponse.createBySuccessMessage("添加分类成功");
	}

	public ServerResponse delCategory(Integer categoryId) {
		if (categoryId == null) {
			return ServerResponse.createByErrorCodeMessage(
					ResponseCode.ILLEGAL_ARGUMENT.getCode(),
					ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		int resultraw = categoryMapper.deleteByPrimaryKey(categoryId);
		if (resultraw == 0) {
			return ServerResponse.createByErrorMessage("删除分类失败");
		}
		return ServerResponse.createBySuccessMessage("删除分类成功");
	}

	public ServerResponse modifyCategory(Category category){
		 category.setUpdateTime(new Date());
		 int resultraw=categoryMapper.updateByPrimaryKeySelective(category);
		 if(resultraw==0){
			return ServerResponse.createByErrorMessage("修改分类失败");
		 }
		 
		 return ServerResponse.createBySuccessMessage("修改分类成功");
	}
	
	
	public ServerResponse<List<Category>> getCategoryListByCategoryId(Integer categoryId){
		if (categoryId == null) {
			return ServerResponse.createByErrorCodeMessage(
					ResponseCode.ILLEGAL_ARGUMENT.getCode(),
					ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
	   List<Category> categoryList= categoryMapper.selectCategoryListByCategoryId(categoryId);
	  if(CollectionUtils.isEmpty(categoryList)){
		  return ServerResponse.createByErrorMessage("改分类下没有其他子类");
	  }
		return ServerResponse.createBySuccess(categoryList);
	}
	

	/**
     * 递归查询本节点的分类及孩子节点的分类
     * @param categoryId
     * @return
     */
	public ServerResponse<List<Category>> getDeepCategoryListByCategoryId(Integer categoryId){
		if (categoryId == null) {
			return ServerResponse.createByErrorCodeMessage(
					ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
		}
	    Set<Category>  categorySet=Sets.newHashSet();
	    findChildCategory(categorySet,categoryId);
	    List<Category> categoryList=Lists.newArrayList();
	    for(Category category:categorySet){
	    	categoryList.add(category);
	    }
	    return ServerResponse.createBySuccess(categoryList);
	}
	
	//递归查询当前id的所有子分类
	private Set<Category>  findChildCategory(Set<Category> categoryset,Integer categoryId){
	
			Category category=categoryMapper.selectByPrimaryKey(categoryId);
			if(category!=null){
				categoryset.add(category);
			}
			 List<Category> categoryList= categoryMapper.selectCategoryListByCategoryId(categoryId);
			 
			 for(Category category2:categoryList){
				 findChildCategory(categoryset, category2.getId());
			 }
		
		return categoryset;
	}
	
	
	public ServerResponse list(Integer mid,Integer pageNum,Integer pageSize){
		if(mid==null){
			return ServerResponse.createByErrorCodeMessage(
					ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
		}
		List<Category> categoryList= categoryMapper.selectCategoryListByMid(mid);
		
		PageHelper pageHelper=new PageHelper();
		pageHelper.startPage(pageNum, pageSize);
		PageInfo pageInfo =new PageInfo(categoryList);
		
		return ServerResponse.createBySuccess(pageInfo);
	}
	
	
}
